#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

string table[128];
int n;
map<string, vector<string>> context;

string encode(string s) {
  string res;
  for (char c : s) res += table[c];
  return res;
}
bool pre_match(string s, string t) {
  int len = s.size();
  rep(i, 0, len - 1) if (s[i] != t[i]) return false;
  return true;
}
void solve(string s) {
  if (context.count(s)) {
    vector<string>& a = context[s];
    cout << a.front();
    if (a.size() > 1) cout << '!';
    cout << endl;
    return;
  }
  int d;
  string ans;
  for (const auto& p : context) {
    const string& code = p.first;
    if (code.size() < s.size() && pre_match(code, s)) {
      int diff = s.size() - code.size();
      if (ans.empty() || diff < d) {
        ans = p.second.front();
        d = diff;
      }
    } else if (code.size() > s.size() && pre_match(s, code)) {
      int diff = code.size() - s.size();
      if (ans.empty() || diff < d) {
        ans = p.second.front();
        d = diff;
      }
    }
  }
  cout << ans << '?' << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // read table
  char c;
  while (cin >> c && c != '*') cin >> table[c];

  // read context
  string t;
  while (cin >> t && t != "*") context[encode(t)].push_back(t);

  // read query
  while (cin >> t && t != "*") solve(t);

  return 0;
}